package b1.scenes.home
{
	import b1.conifg.*;
	import b1.controls.tab.*;
	import b1.net.*;
	import b1.scenes.*;
	import b1.scenes.crime.*;
	import b1.scenes.detect.*;
	import b1.scenes.home.police.*;
	import b1.scenes.home.wanted.*;
	import b1.utils.*;
	
	import flash.display.Sprite;
	import flash.media.Sound;
	
	public class Store extends TabController
	{
		private static var instance:Store;
		
		public function Store()
		{
			var TabControllerBackground:Class = ResourceHelper.getInstance().getClassByName("TabControllerBackground");
			var TabTitleBackground:Class = ResourceHelper.getInstance().getClassByName("TabTitleBackground");
			var CloseButton:Class = ResourceHelper.getInstance().getClassByName("CloseButton1");
			super(TabControllerBackground, CloseButton);
			var tab:Tab = new Tab("骨头", 5, 2);
			this.addTab(TabTitleBackground, tab);
			tab = new Tab("道具", 5, 2);
			this.addTab(TabTitleBackground, tab);
			tab = new Tab("装备", 5, 2);
			this.addTab(TabTitleBackground, tab);
			//除去防具外的所有物体
			var objectItems:Array = new Array();
			//防具（衣服）
			var equipItems:Array = new Array();
			var objectConfig:ObjectConfig;
			for (var i:int = 0; i < Config.objectConfigs.length; i++)
			{
				objectConfig = Config.objectConfigs[i];
				if (objectConfig.onSaleFlag)
				{
					if (objectConfig.type != 2)
					{
						objectItems.push(i);
					}
					else
					{
						equipItems.push(i);
					}
				}
			}
		    objectItems.sort(sortObjects);
		   
			for (var j:int = 0; j < objectItems.length; j++)
			{
				var index:int = objectItems[j];
				objectConfig = Config.objectConfigs[index];
				var storeItem:StoreItem = new StoreItem(objectConfig);
				if (objectConfig.type < 3)
				{
					this.tabs[objectConfig.type].addItem(storeItem, storeItem.width, storeItem.height);
				}
				else
				{
					this.tabs[2].addItem(storeItem, storeItem.width, storeItem.height);
				}
			}
			//防具（衣服）同一套在一起摆放
			equipItems.sort(sortEquipsOnType);
			for (j = 0; j < equipItems.length; j++)
			{
				index = equipItems[j];
				objectConfig = Config.objectConfigs[index];
				storeItem = new StoreItem(objectConfig);
				this.tabs[objectConfig.type].addItem(storeItem, storeItem.width, storeItem.height);
			}
		}

		public static function getInstance():Store
		{
			if (!instance)
				instance = new Store();
			return instance;
		}
		
		public function refresh():void
		{
			for (var i:int = 0; i < this.tabs.length; i++)
			{
				var tab:Tab = this.tabs[i];
				for (var j:int = 0; j < tab.items.length; j++)
				{
					var storeItem:StoreItem = tab.items[j];
					storeItem.refresh();
				}
			}
		}
		
		public function open(parentScene:Sprite):void
		{
			if (!parentScene.contains(this))
			{
				refresh();
				this.x = (BFont.stageWidth - this.width) / 2;
				this.y = 40;
				parentScene.addChild(this);
				//播放音效
				var SoundButton1:Class = ResourceHelper.getInstance().getClassByName("SoundButton1");
				var sound:Sound = new SoundButton1();
				sound.play(); 
			}
			CrimeDialog.getInstance().close();
			DetectDiaglog.getInstance().close();
			WantedTab.getInstance().close();
			PoliceStation.getInstance().close();
			BottomNavigator.getInstance().hide();
		}
		
		public function close():void
		{
			if (this.parent != null)
				this.parent.removeChild(this);
		}
		
	}
}